<template>
  <div>
    <el-dialog
      width="700px"
      :title="$t('标签管理')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <!-- <el-row style="margin-bottom: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="xx">
          <el-input :placeholder="$t('xx')" v-model="queryParams.xx" clearable @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row> -->
      <el-row class="mb10">
        <el-button type="primary" v-hasPermi="['distribution:tag:add']" size="mini" @click="handleAdd">
          {{ $t('新增') }}
        </el-button>
      </el-row>
      <el-table
        :height="calTableHeight() - 100"
        ref="TableRef"
        :data="tableData"
        @selection-change="(rows) => (tableCheck = rows)"
      >
        <el-table-column :selectable="() => true" type="selection" width="55" align="center" />
        <!-- <el-table-column show-overflow-tooltip align="center" :label="$t('ID')" prop="tagId" /> -->
        <el-table-column show-overflow-tooltip align="center" :label="$t('标签')" prop="tagName" />
        <!-- 操作 -->
        <el-table-column :label="$t('操作')" align="center" fixed="right" width="200">
          <template slot-scope="{ row }">
            <el-button type="text" @click="handleEdit(row)" v-hasPermi="['distribution:tag:edit']">
              {{ $t('编辑') }}
            </el-button>
            <el-button type="text" @click="handleDelete(row)" v-hasPermi="['distribution:tag:remove']">
              {{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      /> -->
      <div slot="footer">
        <el-button @click="cancel">
          {{ $t('关闭') }}
        </el-button>
      </div>
    </el-dialog>
    <el-dialog
      width="500px"
      :title="addAndEdit.type == 'add' ? $t('新增') : $t('修改')"
      destroy-on-close
      append-to-body
      :visible.sync="addAndEdit.show"
    >
      <el-form @submit.native.prevent ref="formRef" :model="addAndEdit.form" :rules="addAndEdit.rules">
        <el-form-item :label="$t('标签名')" prop="tagName">
          <el-input v-model="addAndEdit.form.tagName" :placeholder="$t('请输入')" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmitAdd" type="primary" v-if="addAndEdit.type == 'add'">
          {{ $t('确认新增') }}
        </el-button>
        <el-button @click="handleSubmitEdit" type="primary" v-if="addAndEdit.type == 'edit'">
          {{ $t('确认修改') }}
        </el-button>
        <el-button @click="addAndEdit.show = false">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { api } from '@/api/**'
import { addOssTag, ossTagList, deleteOssTag, updateOssTag } from '@/api/distribution/sku/imageSpace'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      addAndEdit: {
        show: false,
        type: '',
        form: {
          tagName: '',
        },
        rules: {
          tagName: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
      },
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleAdd() {
      this.addAndEdit.form.tagName = ''
      this.addAndEdit.type = 'add'

      this.addAndEdit.show = true
    },
    handleSubmitAdd() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          addOssTag(this.addAndEdit.form).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
              this.addAndEdit.show = false
            }
          })
        }
      })
    },
    handleEdit(row) {
      this.addAndEdit.form = { ...row }
      this.addAndEdit.type = 'edit'
      this.addAndEdit.show = true
    },
    handleSubmitEdit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          updateOssTag(this.addAndEdit.form).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
              this.addAndEdit.show = false
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteOssTag(row.tagId).then((res) => {
          this.$message({ type: 'success', message: res.msg })
          this.getList()
        })
      })
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          addOssTag(this.addAndEdit.form).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
            }
          })
        }
      })
    },
    getList() {
      ossTagList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.total = res.total
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
